<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="寺庙名称" prop="templeName">
        <el-input
          v-model="queryParams.templeName"
          placeholder="请输入寺庙名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="寺庙地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入寺庙地址"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:advTemple:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete2"
          v-hasPermi="['system:advTemple:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:advTemple:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="advUpgradeId" /> -->
      <el-table-column label="寺庙名称" align="center" prop="templeName" />
      <el-table-column label="寺届图片" align="center" prop="imageUrl" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="捐款协议" align="center" prop="agreement" show-overflow-tooltip/>
      <el-table-column label="捐款二维码" align="center" prop="qrcodeUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.qrcodeUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:advTemple:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:advTemple:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
   />

    <!-- 添加或修改用户和角色关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="寺庙名称" prop="templeName">
          <el-input v-model="form.templeName" placeholder="请输入寺庙名称" clearable style="width: 220px;"/>
        </el-form-item>
        <el-form-item label="寺庙地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入寺庙地址" />
        </el-form-item>
        <el-form-item label="寺庙图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl" :limit="1"/>
        </el-form-item>
        <el-form-item label="捐款二维码" prop="qrcodeUrl">
          <image-upload v-model="form.qrcodeUrl" :limit="1"/>
        </el-form-item>
        
       
        <el-form-item label="捐款协议" prop="agreement">
          <el-input type="textarea" v-model="form.agreement" placeholder="请输入捐款协议" :rows="10"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProducts, addProducts, updateProducts, delProducts } from '@/api/server/products.js'
import { listTemple, addTemple, updateTemple, delTemple} from '@/api/server/temple.js'
export default {
  dicts: ['adv_products_is_slive'],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productType: 1,
        productName: '',
        productModel: '',
        productDesc: '',
        productPrice: '',
        isSlive: '',
        createTime: '',
        stocknum: '',
      },
      showSearch: true,
      single: false,
      multiple: false,
      loading: false,
      currentPage4: 1,
      open1: false,
      open2: false,
      roleList: [
        
      ],
      total: 0,
      title: "",
      open: false,
      form: {},
      rules: {
        templeName: [
          { required: true, message: "请输入寺庙名称", trigger: "blur" }
        ],
        imageUrl: [
          { required: true, message: "请上传寺庙图片", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入寺庙地址", trigger: "blur" }
        ],
        agreement: [
          { required: true, message: "请上传捐款协议", trigger: "blur" }
        ],
        qrcodeUrl: [
          { required: true, message: "请上传捐款二维码", trigger: "blur" }
        ]
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleQuery() {
      this.getList();
    },
    resetQuery() {},
    handleExport() {
      this.download('/system/advTemple/export', {
        ...this.queryParams
      }, `升级包_${new Date().getTime()}.xlsx`)
    },
    getList() {
      listTemple(this.queryParams).then((response) => {
        this.roleList = response.rows;
        this.total = response.total;
      })
    },
    handleSelectionChange() {},
    cancel() {
      this.open = false;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.advTempleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增寺庙内容";
    },
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改寺庙内容";
    },
    reset() {
      this.form = {
        templeName: "",
        imageUrl: "",
        address: "",
        agreement: "",
        qrcodeUrl: ""
      }
    },
    handleDelete2(){
      this.$confirm('此操作将永久删除此寺庙, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
  }).then(() => {
    if (this.ids.length === 0) {
      this.$message({
        type: 'info',
        message: '请选择要删除的对象'
      });
      return;
    }
    delTemple(this.ids.join(',')).then((response) => {
      this.getList();
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
    }).catch(() => {
      this.$message({
        type: 'error',
        message: '删除失败'
      });
    });
  }).catch(() => {
    this.$message({
      type: 'info',
      message: '已取消删除'
    });
  });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除此寺庙, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(row.advTempleId);
        delTemple(row.advTempleId).then((response) => {
          this.getList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.title === '新增寺庙内容') {
            addTemple(this.form).then((response) => {
              this.getList();
              this.$message({
                type: 'success',
                message: '提交成功!'
              });
              this.open = false;
            })
          }
          if(this.title === '修改寺庙内容') {
            updateTemple(this.form).then((response) => {
              this.getList();
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.open = false;
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: '表单验证失败!'
          });
        }
      })
    },
    
    
  }
}
</script>

<style>

</style>

